package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.entity.Employee;
import com.sky.mapper.EmployeeMapper;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class EmployeeServiceImpl
        extends ServiceImpl<EmployeeMapper, Employee>
        implements EmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;

    @Override
    public EmployeeLoginVO  login(EmployeeLoginDTO dto) {
        //1. 根据用户名查询数据库 select * from employee where username=?
        //使用Wrappers来封装查询条件
        QueryWrapper<Employee> wrapper = Wrappers.<Employee>query()
                .eq("username", dto.getUsername());
        Employee employee = employeeMapper.selectOne(wrapper);
        if(employee==null){
            //说明用户名不存在
            throw  new RuntimeException("用户名不存在");
        }

        //2. 将dto中的密码和数据库中的密码做对比
        if(!employee.getPassword().equals(dto.getPassword())){
            throw  new RuntimeException("密码错误");
        }

        //3. 判断用户的状态
        if(employee.getStatus() == 0){
            throw  new RuntimeException("用户已被禁用");
        }

        //4. 生成jwt令牌
        Map<String, Object> map = new HashMap<>();
        map.put("empId", employee.getId());
        String token = JwtUtil.createJWT(map);

        //5. 返回数据
        return EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();
    }

}
